package com.back.controller;


import com.back.model.reslut.Result;
import com.back.service.tool.MinioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Resource
    private MinioService minioService;

    @PostMapping("/uploadFile")
    public Result<String> uploadFile(@ RequestParam("file") MultipartFile File) {
        if (File.isEmpty()) {
            throw new RuntimeException("文件为空");
        }
        log.info("----------------------------");
        log.info("getName-->"+File.getName());
        log.info("getOriginalFilename-->"+File.getOriginalFilename());
        log.info("getContentType-->"+File.getContentType());
        log.info("getSize-->"+File.getSize());
        log.info("getResource-->"+File.getResource());
        log.info("----------------------------");
        String url = minioService.uploadFile(File);
        return Result.successData(url);
    }


    @GetMapping("/downloadFile")
    public void uploadFile(String fileName, HttpServletResponse response) {
        minioService.downloadFile(fileName,response);
    }


    @GetMapping("/deleteFile")
    public  Result<String> deleteFile(String fileName) {
        minioService.deleteFile(fileName);
        return Result.successMessage("删除成功");

    }



}
